package com.hospital.hospitalserver.service;

import com.hospital.hospitalserver.entity.GenerateScheduleRequest;
import com.hospital.hospitalserver.entity.ScheduleAIRequest;
import com.hospital.hospitalserver.entity.ScheduleInstanceDTO;
import com.hospital.hospitalserver.entity.ScheduleTemplate;
import com.hospital.hospitalserver.mapper.ScheduleMapper;
import com.hospital.hospitalserver.util.Pager;
import com.hospital.hospitalserver.util.PagerData;
import com.hospital.hospitalserver.util.Response;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 排班管理功能实现类
 * */
@Service
@RequiredArgsConstructor
public class ScheduleService {
    private final ScheduleMapper scheduleMapper;

    /**
     * 人员类型定义表信息
     * */
    public Response getStaffTypeList(){
        return Response.success(scheduleMapper.getStaffTypeList());
    }

    /**
     * 班次时间定义表信息
     * */
    public Response getTimeSlotList(){
        return Response.success(scheduleMapper.getTimeSlotList());
    }

    /**
     * 排班类型定义表信息
     * */
    public Response getScheduleTypeList(){
        return Response.success(scheduleMapper.getScheduleTypeList());
    }

    /**
     * 添加排班模板
     * */
    public Response addScheduleTemplate(ScheduleTemplate scheduleTemplate){
        System.out.println(scheduleTemplate);
        Integer count = scheduleMapper.addScheduleTemplate(scheduleTemplate);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }


    /**
     * 编辑排班模板
     * */
    public Response editScheduleTemplate(ScheduleTemplate scheduleTemplate){
        Integer count = scheduleMapper.editScheduleTemplate(scheduleTemplate);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 删除排班模板
     * */
    public Response deleteScheduleTemplate(ScheduleTemplate scheduleTemplate){
        Integer count = scheduleMapper.deleteScheduleTemplate(scheduleTemplate);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 排班模板数据统计
     * */

    /**
     * 排班模板分页功能
     * 获取排班数据总数
     * */
    private Integer getScheduleTemplateCountsBySearch(Pager pager){
        return scheduleMapper.getScheduleTemplateCountsBySearch(pager);
    }

    /**
     * 计算分页
     * */
    private Integer getScheduleTemplatePageTotalBySearch(Pager pager){
        Integer scheduleTemplateTotalCounts = getScheduleTemplateCountsBySearch(pager);
        Integer pageTotal = 1;//设置初始页码为1
        //数据总条数除以pageSize每页显示条数，如取余数求模，如果有余数，页码+1
        //如果可以整除，则页码等于数据总数除以pageSize
        pageTotal = scheduleTemplateTotalCounts/pager.getPageSize();
        if(scheduleTemplateTotalCounts%pager.getPageSize()>0){
            pageTotal += 1;
        }
        return pageTotal;
    }

    /**
     * 排班模板分页功能
     * 获取排班模板的分页数据
     * */
    private List<ScheduleTemplate> getScheduleTemplateMapperDataBySearch(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1)*pager.getPageSize();
        pager.setStartIndex(startIndex);
        return scheduleMapper.getScheduleTemplateMapperDataBySearch(pager);
    }

    /**
     * 装载排班模板分页数据信息给前端API
     * */
    public Response getScheduleTemplatePagerDataBySearch(Pager pager){
        PagerData pagerData = new PagerData();
        pagerData.setTotal(getScheduleTemplateCountsBySearch(pager));
        pagerData.setPageTotal(getScheduleTemplatePageTotalBySearch(pager));
        pagerData.setData(getScheduleTemplateMapperDataBySearch(pager));
        pagerData.setDataStatus(scheduleMapper.getScheduleTemplateDataStatus());
        return Response.success(pagerData);
    }

    /**
     * 获取排班模板列表数据用于排班
     * */
    public Response getScheduleTemplateList(){
        return Response.success(scheduleMapper.getScheduleTemplateList());
    }

    /**
     * 根据AI工作流分析的关键词查询排班模板数据
     * */
    public Response queryScheduleTemplate(ScheduleAIRequest request){
        return Response.success(scheduleMapper.queryScheduleTemplate(request));
    }
}
